# tts_processor.py
import edge_tts
import os
import uuid
import pygame
from config.config import Config
class EdgeTTS:
    """语音合成类"""
    def __init__(self, config):
        self.config = config
        os.makedirs(config.TEMP_DIR, exist_ok=True)
        pygame.mixer.init()

    async def synthesize(self, text):
        """文本转语音并播放"""
        try:
            filename = f"{uuid.uuid4()}.mp3"
            output_path = os.path.join(self.config.TEMP_DIR, filename)

            communicate = edge_tts.Communicate(text, self.config.VOICE_NAME)
            await communicate.save(output_path)

            if os.path.exists(output_path):
                pygame.mixer.music.load(output_path)
                pygame.mixer.music.play()
                while pygame.mixer.music.get_busy():
                    pygame.time.Clock().tick(10)
        except Exception as e:
            print("语音生成失败:", str(e))